/*
猫猫 api 接口
其中 编辑猫猫信息 函数未使用
上传猫猫图片函数在 upload 函数中实现
*/

package v1

import (
    "net/http"
    "strconv"

    "gitee.com/keeving/quanta-test-cat/model"
    "gitee.com/keeving/quanta-test-cat/utils/errmsg"
    "github.com/gin-gonic/gin"
)

// 获取单个猫猫
func GetCat(c *gin.Context) {
       id, _ := strconv.Atoi(c.Param("kid"))
       data, code := model.GetCat(id)
       c.JSON(http.StatusOK, gin.H{
              "code":  code,
              "data":    data,
              "msg": errmsg.GetErrMsg(code),
       })
}

// 新增猫猫
func AddCat(c *gin.Context) {
       var data model.Cat

       // 获取表单数据
       name := c.PostForm("name")
       sexStr := c.PostForm("sex")
       color := c.PostForm("color")
       brief := c.PostForm("brief")
       adoption := c.PostForm("adoption")
       sterilization := c.PostForm("sterilization")
       guide := c.PostForm("guide")

       // 将sexStr转换为int
       sex, err := strconv.Atoi(sexStr)
       if err != nil {
           c.JSON(http.StatusBadRequest, gin.H{
               "code":  errmsg.ERROR,
               "msg":   "性别参数格式错误",
           })
           return
       }

       // 赋值给data
       data.Name = name
       data.Sex = sex
       data.Color = color
       data.Brief = brief
       data.Adoption = adoption
       data.Sterilization = sterilization
       data.Guide = guide
       data.Img = UploadFile(c)
       data.ImgNumber = 1

       code := model.CreateCat(&data)

       c.JSON(http.StatusOK, gin.H{
              "code":  code,
              "data":  data,
              "msg":   errmsg.GetErrMsg(code),
       })
}
// 更新猫猫
// func UpdateCat(c *gin.Context) {
//        var data model.Cat
//        id, _ := strconv.Atoi(c.Param("id"))
//        _ = c.ShouldBindJSON(&data)

//        code := model.UpdateCat(id, &data)

//        c.JSON(http.StatusOK, gin.H{
//               "status":  code,
//               "message": errmsg.GetErrMsg(code),
//        })
// }

// 获取猫猫列表
func GetCats(c *gin.Context) {
       pageSize, _ := strconv.Atoi(c.Query("pageSize"))
       pageNum, _ := strconv.Atoi(c.Query("curPage"))
       // username := c.Query("username")

       switch {
       case pageSize >= 100:
              pageSize = 100
       case pageSize <= 0:
              pageSize = 10
       }

       if pageNum == 0 {
              pageNum = 1
       }

       data, total := model.GetCats(0, pageSize, pageNum)

       code := errmsg.SUCCESS
       c.JSON(http.StatusOK, gin.H{
              "code":  code,
              "msg": errmsg.GetErrMsg(code),
              "data":  gin.H{
                     "totalPages": total,
                     "curPage":     pageNum,
                     "ketties":     data,
                     },
              },
       )
}

// 获取猫猫相册
func GetCatImgs(c *gin.Context) {
       id, _ := strconv.Atoi(c.Param("kid"))
       data, code := model.GetCatImgs(id)
       c.JSON(http.StatusOK, gin.H{
              "code":  code,
              "data":    data,
              "msg": errmsg.GetErrMsg(code),
      })
}

// 删除猫猫
func DeleteCat(c *gin.Context) {
       id, _ := strconv.Atoi(c.Param("kid"))

       code := model.DeleteCat(id)

       c.JSON(http.StatusOK, gin.H{
              "code":  code,
              "msg": errmsg.GetErrMsg(code),
              "data":    nil,
       })
}

// 模糊搜索猫猫
func SearchCat(c *gin.Context) {
       name := c.Query("name")
       data, total, code := model.SearchCat(name)
       c.JSON(
              http.StatusOK, gin.H{
                     "code":  code,
                     "data": map[string]interface{}{
                            "kettyNumber": total,
                            "ketties":     data,
                        },
                     "msg": errmsg.GetErrMsg(code),
              },
       )
}

// 上传猫猫图片
// 见 UploadCat